package cn.buildoncloud.labor.workermanapplet.business.modules.services.impl;

import cn.buildoncloud.labor.workermanapplet.business.modules.services.SpecialPersonService;
import cn.buildoncloud.labor.workermanapplet.common.base.service.BaseServiceImpl;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.entity.Record;
import org.nutz.dao.sql.Sql;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SpecialPersonServiceImpl extends BaseServiceImpl<Map> implements SpecialPersonService {

    public SpecialPersonServiceImpl(Dao dao) {
        super(dao);
    }

    @Override
    public List<Record> listSpecialPerson(String search, Long pId) {
        StringBuilder sb = new StringBuilder(" SELECT CONCAT(b_s_p.id ,'') id , b_s_p. NAME, s_p_j.`name` job_name, b_s_p.tel, CONCAT(b_s_p.head_photo_id ,'') head_photo_id  ");
        sb.append(" FROM bi_special_person b_s_p LEFT JOIN sys_project_job s_p_j ON b_s_p.job_id = s_p_j.id ");
        sb.append(" WHERE b_s_p.project_id = @projectId and b_s_p.delFlag = 0 ");
        if (StringUtils.hasText(search)){
            sb.append(" and (b_s_p. NAME LIKE @search ")
                    .append(" OR b_s_p.tel like @search ")
                    .append(" OR b_s_p.id_card_no like @search ")
                    .append(" OR s_p_j.`name` like @search) ");
        }
        Sql sql = Sqls.create(sb.toString());
        sql.setParam("projectId", pId);
        if (StringUtils.hasText(search)){
            sql.setParam("search", "%" + search + "%");
        }
        return this.list(sql);
    }

    @Override
    public Map<String, Object> getSpecialPerson(Long id) {
        StringBuilder sb = new StringBuilder(" SELECT CONCAT( b_s_p.id ,'') id , b_s_p. NAME, b_s_p.id_card_no, b_s_p.tel, b_s_p.birth_date, TIMESTAMPDIFF(YEAR, b_s_p.birth_date, CURDATE()) age, b_s_p.sex, b_s_p.nation, s_p_j.`name` job_name, CONCAT(b_s_p.id_card_photo_id, '') id_card_photo_id, b_s_p.address, CONCAT(b_s_p.head_photo_id ,'') head_photo_id ");
        sb.append(" FROM bi_special_person b_s_p ")
                .append(" LEFT JOIN sys_project_job s_p_j ON b_s_p.job_id = s_p_j.id ")
                .append(" WHERE b_s_p.id = @id");

        Sql sql = Sqls.create(sb.toString());
        sql.setParam("id", id);
        List<Record> recordList = this.list(sql);
        if (recordList == null){
            return null;
        }
        if (recordList.size() == 0){
            return null;
        }
        return recordList.get(0);
    }

    @Override
    public Map<String, Object> getSpecialPersonIdByIdCardNo(String idCardNo) {
        StringBuilder sb = new StringBuilder("SELECT CONCAT(id,'') id FROM bi_special_person where id_card_no = @idCardNo");
        Sql sql = Sqls.create(sb.toString());
        sql.setParam("idCardNo", idCardNo);
        if (this.list(sql).size() > 0){
            return this.list(sql).get(0);
        }
        return null;
    }

    @Override
    public List<Record> listJob() {
        StringBuilder sb = new StringBuilder(" SELECT CONCAT(id,'') id, `name` FROM sys_project_job");
        Sql sql = Sqls.create(sb.toString());
        return this.list(sql);
    }

    @Override
    public Map<String, Object> getSpecialPersonHeadPhotoById(Long id) {
        StringBuilder sb = new StringBuilder(" SELECT CONCAT(head_photo_id,'') head_photo_id FROM bi_special_person where id = @id ");
        Sql sql = Sqls.create(sb.toString());
        sql.setParam("id", id);
        List<Record> result = this.list(sql);
        if (result.size() > 0){
            return result.get(0);
        }
        return new HashMap<>();
    }


//    @Override
//    public void SpecialPersonOutProject(Long id) {
//        //UPDATE bi_special_person SET project_id = NULL WHERE id = ?
//        StringBuilder sb = new StringBuilder(" UPDATE bi_special_person SET project_id = NULL WHERE id = @id");
//        Sql sql = Sqls.create(sb.toString());
//        sql.setParam("id", id);
//        this.execute(sql);
//    }


    /**
     *@create 2019/1/16 12:12
     *@Param [pId, idCardNo]
     *@return java.util.Map<java.lang.String,java.lang.Object>
     *@desc  根据身份证号和项目id  查询 工程人员
     */
    @Override
    public Map<String, Object> selectSepcialPersonByPidAndIdCardNo(String pId, String idCardNo) {
        StringBuilder sb = new StringBuilder("SELECT CONCAT(id,'') id FROM bi_special_person where  project_id =@pId and id_card_no = @idCardNo  and  delFlag = 0 ");
        Sql sql = Sqls.create(sb.toString());
        sql.setParam("pId",pId);
        sql.setParam("idCardNo", idCardNo);
        if (this.list(sql).size() > 0){
            Record record = this.list(sql).get(0);
            return this.list(sql).get(0);
        }
        return null;
    }
}
